﻿using Nutrapp.DBAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Nutrapp.Controllers
{
   
    public class IngredientsController : Controller
    {
        //
        // GET: /Ingredients/

        private NutrappDataContext context = new NutrappDataContext();

        public ActionResult Index()
        {
            var ingredients = context.Ingredients.Select(row => row);
            return View(ingredients);
        }

        public ActionResult Create()
        {
            NutrappDataContext context = new NutrappDataContext();
            ViewBag.ID = new SelectList(context.Ingredients_Categories, "ID", "Name");
            var model = new Ingredient();
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            Ingredient ingr = context.Ingredients.Where(i => i.ID == id).FirstOrDefault();

            return View(ingr);
        }

        [HttpPost]
        public ActionResult Create(Ingredient newIngredient)
        {

            Session["existIngrError"] = "";
            Session["nameError"] = "";
            Session["carboError"] = "";

                bool exist = false;
                List<String> ingrNameList = context.Ingredients.Select(i => i.Name).ToList();
                foreach (String ingrName in ingrNameList)
                {
                    if (ingrName == newIngredient.Name)
                    {
                        exist = true;
                        Session["existIngrError"] = newIngredient.Name.ToString() + " already exists!";
                    }
                }

                if (newIngredient.Name == null)
                {
                    Session["nameError"] = "Ingredient Name must contain only lettersr, it must contain at least 4 characters and no more than 15 characters ";
                }
                else
                {
                    if (newIngredient.Category == null)
                    {

                        Session["nameError"] = "Choose category";
                    }
                    else
                    {
                        var regexItem = new Regex("^[a-zA-Z]*$");

                        if (!regexItem.IsMatch(newIngredient.Name) || newIngredient.Name.Length > 15 || newIngredient.Name.Length < 3)
                        {
                            Session["nameError"] = "Ingredient Name must contain only lettersr, it must contain at least 4 characters and no more than 15 characters ";
                        }



                        if (newIngredient.Carbohydrate > 1000000 || newIngredient.Calories > 1000000 ||
                            newIngredient.Fat > 1000000 || newIngredient.PeiceToGram > 1000000 || newIngredient.Protein > 1000000)
                        {
                            Session["carboError"] = "The input for Carbohydrate, Protein,Calories,Fat and Grams in peice must contain only numbers and be not longer than 7 characters";
                        }
                    }
                }

                if (exist == false & Session["nameError"].ToString() == "" & Session["carboError"].ToString() == "")
                {
                    context.Ingredients.InsertOnSubmit(newIngredient);
                    context.SubmitChanges();
                    return RedirectToAction("Index");
                }


                else
                {
                    ViewBag.ID = new SelectList(context.Ingredients_Categories, "ID", "Name");
                   
                    return View(newIngredient);
                }
            }
        

        [HttpPost]
        public ActionResult Edit(Ingredient ingredient)
        {
            using (NutrappDataContext context = new NutrappDataContext())
            {
                Ingredient ingr = context.Ingredients.Where(i => i.ID == ingredient.ID).FirstOrDefault();
                ingr.Name = ingredient.Name;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        [HandleError]
        public ActionResult Delete(Ingredient ingt)
        {
            try
            {
                using (NutrappDataContext context = new NutrappDataContext())
                {
                    Ingredient ingredient = (Ingredient)context.Ingredients.Where(ing => ing.ID == ingt.ID).FirstOrDefault();
                    context.Ingredients.DeleteOnSubmit(ingredient);
                    context.SubmitChanges();

                }
            }
            catch (Exception e)
            {

                Session["deleteError"] = "Error!";
            }

            return RedirectToAction("Index", "Ingredients");
        }

        [HttpGet]
        public ActionResult Delete()
        {
            return View();
        }
    }

}
